package boun.swe.bob.dao;

import boun.swe.bob.entities.Like;
import boun.swe.bob.entities.User;
import boun.swe.bob.entities.UserAction;
import boun.swe.bob.entities.UserTweet;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Component;

import java.sql.SQLException;
import java.util.List;

/**
 * This DAO corresponds to likes table in database
 */
@Component
public class LikesDao extends GenericDaoImpl<Like, Integer>{

    private static final Logger LOG = Logger.getLogger(LikesDao.class);

    /**
     * This method is used for deleting a like action
     *
     * @param likerUserId corresponds to user id in likes database
     * @param actionId corresponds to action_id in user_actions table
     */
    public void deleteLike(final int likerUserId, final int actionId) {
        hibernateTemplate.execute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException, SQLException {
                Query query = session.createSQLQuery("delete from likes where liker_user_id = ? and action_id = ?");
                query.setInteger(0, likerUserId);
                query.setInteger(1, actionId);
                query.executeUpdate();
                return null;
            }
        });
    }

}
